package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.MemberType;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VMemberType;
import com.jxc.service.MemberTypeServiceI;

@Service("memberTypeService")
public class MemberTypeServiceImpl implements MemberTypeServiceI {

	@Autowired
	private BaseDaoI<MemberType> memberTypeDao;

	@Override
	public DataGrid datagrid(VMemberType vMemberType) {
		DataGrid j = new DataGrid();
		j.setRows(this.changeModel(this.find(vMemberType)));
		j.setTotal(this.total(vMemberType));
		return j;
	}

	private List<MemberType> find(VMemberType vMemberType) {
		String hql = "from MemberType t where t.isDeleted=false ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vMemberType, hql, values);

		if (vMemberType.getSort() != null && vMemberType.getOrder() != null) {
			hql += " order by " + vMemberType.getSort() + " "
					+ vMemberType.getOrder();
		}
		return memberTypeDao.find(hql, values, vMemberType.getPage(),
				vMemberType.getRows());
	}

	private List<VMemberType> changeModel(List<MemberType> memberTypes) {
		List<VMemberType> vMemberTypes = new ArrayList<VMemberType>();
		if (memberTypes != null && memberTypes.size() > 0) {
			for (MemberType mt : memberTypes) {
				VMemberType vMemberType = new VMemberType();
				BeanUtils.copyProperties(mt, vMemberType);

				if (mt.getUser() != null) {
					User user = mt.getUser();
					vMemberType.setUid(user.getId());
					vMemberType.setUname(user.getUsername());
				}

				vMemberTypes.add(vMemberType);
			}
		}
		return vMemberTypes;
	}

	private Long total(VMemberType vMemberType) {
		String hql = "select count(*) from MemberType t where t.isDeleted=false ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vMemberType, hql, values);
		return memberTypeDao.count(hql, values);
	}

	private String addWhere(VMemberType vMemberType, String hql,
			List<Object> values) {
		if (vMemberType.getUid() != null) {
			hql += " and t.user.id=? ";
			values.add(vMemberType.getUid());
		}
		return hql;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					MemberType mt = memberTypeDao.get(MemberType.class, Long
							.valueOf(id.trim()));
					if (mt != null) {
						mt.setIsDeleted(true);
						memberTypeDao.update(mt);
					}
				}
			}
		}
	}

	@Override
	public void save(VMemberType vMemberType) {
		MemberType mt = new MemberType();
		BeanUtils.copyProperties(vMemberType, mt);
		if (vMemberType.getUid() != null) {
			User user = new User();
			user.setId(vMemberType.getUid());
			mt.setUser(user);
		}
		mt.setIsDeleted(false);
		memberTypeDao.save(mt);

	}

	@Override
	public void update(VMemberType vMemberType) {
		MemberType mt = new MemberType();
		BeanUtils.copyProperties(vMemberType, mt);
		if (vMemberType.getUid() != null) {
			User user = new User();
			user.setId(vMemberType.getUid());
			mt.setUser(user);
		}
		mt.setIsDeleted(false);
		memberTypeDao.update(mt);

	}

	@Override
	public List<VMemberType> combobox(VMemberType vMemberType) {
		String hql = "from MemberType t where t.isDeleted=false and t.user.id=?";
		List<Object> values = new ArrayList<Object>();
		values.add(vMemberType.getUid());
		List<MemberType> list = memberTypeDao.find(hql, values);
		return changeModel(list);
	}

}
